/*  This file is part of LitSearch.

    LitSearch is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    LitSearch is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with LitSearch; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

package de.fhbrs.litsearch;

import com.softtech.jdbc.SQLResults;

import fedora.server.types.gen.ObjectFields;

import java.lang.StringBuffer;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Creates various exports (e.g CSV, BibTex and a print view)
 *
 * @version 	1.2 22 Oct 2006
 * @author 		Alexander Schaer, Marco Werner
 * 
 */

public class ExportBean {
	private StringBuffer resultBuffer;
	private String resultString;
	private LanguageBean langObject;

	/** 
     * Set all instance variables to default values
     */
	public ExportBean() {
	this.resultBuffer = new StringBuffer();
	this.resultString = "";
	}
	
	public void setLanguage(LanguageBean object){
		this.langObject=object;
	}
	
	public String getLanguage(){
		return this.langObject.getLanguage();
	}
	
	/** 
     * This method converts a given author string into a string array
     * @param x the given author string which is convierted
     * @return a string array of authors
     */
	public String[] convertToArray(String x){
		
		String regex = "\",\"";
		String [] carved = Pattern.compile(regex).split(x);
		;
		carved[0]=carved[0].substring(1,carved[0].length());
		carved[carved.length-1]=carved[carved.length-1].substring(0,carved[carved.length-1].length()-1);
		return carved;
	}
	
	/** 
     * This method presents the authors of a string array
     * @param a string array which contains the authors
     * @return a structured author string
     */
	public String showAuthorArray(String[] authors){
		String myString="";
		for(int i=0;i<authors.length;i++){	
			if (authors[i].length() > 0) {
				myString+=authors[i]+"; ";	
			}
		}
		if (myString.length() >= 2) {
			myString=myString.substring(0,myString.length()-2);
		}
		return myString;
	}
	
	/** 
     * Create a table header for the CSV export
     * @return Table header (e.g. Author,Title,Year, etc.)
     */
	public StringBuffer buildCSVHeader() {
		this.resultBuffer.append(this.langObject.getTextFor("search.Signature")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Editor")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Year")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Title")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Title2")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Institute")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Address")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Shelf")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Volume")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Remarks")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Pages")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Author")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.BibTexShort")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Publisher")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Journal")+";");
		this.resultBuffer.append(this.langObject.getTextFor("search.Room")+";");
		this.resultBuffer.append("\n");
		this.resultBuffer.append("\n");
		return resultBuffer;
	}
	
	/** 
     * Generate an ID for the BibTex entries
     * @param an object of the class MediumBean
     * @return the generated ID
     */
	public String generateBibTexID(MediumBean medium){
		return String.valueOf(medium.getId());
	}
	
	/** 
     * This method creates the CSV export with the given informations of the MediumBean object
     * @param an object of the class MediumBean
     * @return a CSV export for one medium
     */
	public String exportCSVDetails(MediumBean medium) {
		this.buildCSVHeader();
		this.resultBuffer.append(medium.getSignaturPrint()+";");
		this.resultBuffer.append(medium.getEditorPrint()+";");
		this.resultBuffer.append(medium.getYearPrint()+";");
		this.resultBuffer.append(medium.getTitle1Print()+";");
		this.resultBuffer.append(medium.getTitle2Print()+";");
		this.resultBuffer.append(medium.getInstitutePrint()+";");
		this.resultBuffer.append(medium.getAddressPrint()+";");
		this.resultBuffer.append(medium.getShelfPrint()+";");
		this.resultBuffer.append(medium.getVolPrint()+";");
		this.resultBuffer.append(medium.getRemarksPrint()+";");
		this.resultBuffer.append(medium.getPagePrint()+";");
		this.resultBuffer.append(medium.getAuthorsPrint()+";");
		this.resultBuffer.append(medium.getBibtexkuerzelPrint()+";");
		this.resultBuffer.append(medium.getPublisherPrint()+";");
		this.resultBuffer.append(medium.getJournalPrint()+";");
		this.resultBuffer.append(medium.getRoomPrint()+";");		
		this.resultBuffer.append("\n");
		
		this.resultString=resultBuffer.toString();
		return resultString;
	}
	
	/** 
     * This method creates the CSV export for the results given by a search query
     * @param an object of the class SearchBean
     * @return a CSV export for a search query
     */
	public String exportCSVSearch(SearchBean search)	{
		if (search.getAnzSearchResults() == 0)
			return this.langObject.getTextFor("search.noResults");		
		
		try{
			search.doSearch();
			this.buildCSVHeader();
			List<ObjectFields> searchRes = search.getObjects(); 
			MediumBean tmp;
			String id;
		for (int row=0; row < searchRes.size(); row++) {
				tmp = new MediumBean();
				id = (searchRes.get(row).getPid().split(":"))[1];
				tmp.initializeMedium(id);

				this.resultBuffer.append(tmp.getSignaturPrint().toString()+";");
				this.resultBuffer.append(tmp.getEditorPrint().toString()+";");
				this.resultBuffer.append(tmp.getYearPrint().toString()+";");
				this.resultBuffer.append(tmp.getTitle1Print().toString()+";");
				this.resultBuffer.append(tmp.getTitle2Print().toString()+";");
				this.resultBuffer.append(tmp.getInstitutePrint().toString()+";");
				this.resultBuffer.append(tmp.getAddressPrint().toString()+";");
				this.resultBuffer.append(tmp.getShelfPrint().toString()+";");
				this.resultBuffer.append(tmp.getVolPrint().toString()+";");
				this.resultBuffer.append(tmp.getRemarksPrint()+";");
				this.resultBuffer.append(tmp.getPagePrint()+";");
				this.resultBuffer.append(tmp.getAuthorsPrint()+";");
				this.resultBuffer.append(tmp.getBibtexkuerzelPrint()+";");
				this.resultBuffer.append(tmp.getPublisherPrint()+";");
				this.resultBuffer.append(tmp.getJournalPrint()+";");
				this.resultBuffer.append(tmp.getInventoryNumberPrint()+";");
				this.resultBuffer.append(tmp.getRoomPrint()+";");
				
				this.resultBuffer.append("\n");
		}
		}catch(Exception e){
			search.setErrorMessage(e.toString());
		}
		this.resultString=resultBuffer.toString();
		return resultString;
	}

	/** 
     * This method creates the CSV export for all wallet items
     * @param an object of the class SearchBean
     * @return a CSV export for wallet entries
     */
	public String exportCSVWallet(SearchBean search) {
		
		if(search.getWallet().isEmpty()){
			return "<h1>"+this.langObject.getTextFor("wallet.empty")+"</h1>";
		}
		else {
			
			MediumBean tmp = new MediumBean();
			tmp.setSortWallet(search.getOrderBy());
			
			search.setAnzWalletResults(search.walletSize());
			
			
			Vector<MediumBean> walletElements = new Vector<MediumBean>();
			
			for (int row=0; row < search.getAnzWalletResults(); row++){
				tmp.initializeMedium(search.getWallet().get(row).toString());
				tmp.setSortWallet(search.getOrderBy());
				walletElements.add(tmp);
				tmp = new MediumBean();
			}
			
			Collections.sort(walletElements);
			int row = 0;
			int max = 0;
			int inc = 0;
			int i = 0;
			
			switch(search.getOrderDirection()){
				case 0:	//asc
					row = 0;
					i = 0;
					max = walletElements.size();
					inc = 1;
					break;
				case 1: //desc
					row = walletElements.size()-1;
					i = -walletElements.size();
					max = 0;
					inc = -1;
					break;
				default:
					row = 0;
					i = 0;
					max = walletElements.size();
					inc = 1;
			}
			
			this.buildCSVHeader();
			for (; i < max; i++,row=row+inc){
				tmp = walletElements.get(row);
				
				this.resultBuffer.append(tmp.getSignaturPrint()+";");
				this.resultBuffer.append(tmp.getEditorPrint()+";");
				this.resultBuffer.append(tmp.getYearPrint()+";");
				this.resultBuffer.append(tmp.getTitle1Print()+";");
				this.resultBuffer.append(tmp.getTitle2Print()+";");
				this.resultBuffer.append(tmp.getInstitutePrint()+";");
				this.resultBuffer.append(tmp.getAddressPrint()+";");
				this.resultBuffer.append(tmp.getShelfPrint()+";");
				this.resultBuffer.append(tmp.getVolPrint()+";");
				this.resultBuffer.append(tmp.getRemarksPrint()+";");
				this.resultBuffer.append(tmp.getPagePrint()+";");
				this.resultBuffer.append(tmp.getAuthorsPrint()+";");
				this.resultBuffer.append(tmp.getBibtexkuerzelPrint()+";");
				this.resultBuffer.append(tmp.getPublisherPrint()+";");
				this.resultBuffer.append(tmp.getJournalPrint()+";");
				this.resultBuffer.append(tmp.getInventoryNumberPrint()+";");
				this.resultBuffer.append(tmp.getRoomPrint()+";");
				this.resultBuffer.append("\n");
			}
	
			this.resultString=resultBuffer.toString();
			return resultString;
		}
	}
	
	/** 
     * This method creates a print version for a specific medium
     * @param an object of the class MediumBean
     * @return a print version of the given informations from the MediumBean object
     */
	public String exportPrintDetails(MediumBean medium) {
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Title")+": "+medium.getTitle1Print()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Author")+": "+medium.getAuthorsPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Editor")+": "+medium.getEditorPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Signature")+": "+medium.getSignaturPrint()+"</p>");
		//this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Edition")+": "+medium.getEditionPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Year")+": "+medium.getYearPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Title2")+": "+medium.getTitle2Print()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Institute")+": "+medium.getInstitutePrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Address")+": "+medium.getAddressPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Shelf")+": "+medium.getShelfPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Journal")+": "+medium.getJournalPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Volume")+": "+medium.getVolPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Remarks")+": "+medium.getRemarksPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Pages")+": "+medium.getPagePrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Litart")+": "+medium.getLitartPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.BibTexShort")+": "+medium.getBibtexkuerzelPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Publisher")+": "+medium.getPublisherPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Journal")+": "+medium.getJournalPrint()+"</p>");
		this.resultBuffer.append("<p>"+this.langObject.getTextFor("search.Room")+": "+medium.getJournalPrint()+"</p>");
		this.resultString=resultBuffer.toString();
		return resultString;
	}
	
	/** 
     * This method creates a print version of the results for a given search query
     * @param an object of the class SearchBean
     * @return a print version for a search result set
     */
	public String exportPrintSearch(SearchBean search)	{
		if (search.getAnzSearchResults() == 0)
			return this.langObject.getTextFor("search.noResults");
		
		
		JDBCBean dbCon= new JDBCBean();

		try{
			search.doSearch();
			List<ObjectFields> searchRes = search.getObjects(); 
			MediumBean tmp;
			String id;
				
			Calendar rightNow = Calendar.getInstance();
			String minute="";
			if (rightNow.get(Calendar.MINUTE)<10) {
				minute ="0"+rightNow.get(Calendar.MINUTE);
			}
			else {
				minute =String.valueOf(rightNow.get(Calendar.MINUTE));
			}
		
			int monat = rightNow.get(Calendar.MONTH) + 1;
			
			this.resultBuffer.append("<h3>");
			this.resultBuffer.append(this.langObject.getTextFor("header.title"));
			this.resultBuffer.append(this.langObject.getTextFor("export.printDate")+ rightNow.get(Calendar.DATE) + "." + monat + "." + rightNow.get(Calendar.YEAR));
			this.resultBuffer.append(this.langObject.getTextFor("export.printTime")+ rightNow.get(Calendar.HOUR_OF_DAY)+ ":"+ minute);
			this.resultBuffer.append("</h3>");
			this.resultBuffer.append("<ol>");
			for (int row=0; row < searchRes.size(); row++) {
				tmp = new MediumBean();
				id = (searchRes.get(row).getPid().split(":"))[1];
				tmp.initializeMedium(id);
				tmp.setLangObject(this.langObject);
				this.resultBuffer.append("<li>");
				if (tmp.getLitartPrint().equals("greylit"))
					this.resultBuffer.append(this.langObject.getTextFor("export.greylit")+",  ");
				if (tmp.getLitartPrint().equals("book"))
					this.resultBuffer.append(this.langObject.getTextFor("export.book")+",  ");
				if (tmp.getLitartPrint().equals("artikel"))
					this.resultBuffer.append(this.langObject.getTextFor("export.article")+",  ");
				this.resultBuffer.append("<strong>");
				this.resultBuffer.append(tmp.getAuthorsPrint());
				this.resultBuffer.append("</strong>");
				if (!tmp.getTitle1Print().equals("")) this.resultBuffer.append(",  "+tmp.getTitle1Print());
				if (!tmp.getYearPrint().equals("")) this.resultBuffer.append(",  "+tmp.getYearPrint());
				if (!tmp.getSignaturPrint().equals("")) this.resultBuffer.append(",  "+tmp.getSignaturPrint());
				if (!tmp.getIsbnPrint().equals("")) this.resultBuffer.append(",  "+tmp.getIsbnPrint());
				if (!tmp.getJournalPrint().equals("")) {
					SQLResults res2 = dbCon.getResultSet("select name from journals where shortcut='"+tmp.getJournalPrint()+"'");
					if (res2.getRowCount() > 0) this.resultBuffer.append(",  in: "+res2.getString(0,"name"));
				}
				this.resultBuffer.append("</li>");
				this.resultBuffer.append("<br>");
			}
			this.resultBuffer.append("</ol>");
			this.resultBuffer.append("<h1>");
		}catch(Exception e){
			System.out.println("Fehler: "+e.toString());
			search.setErrorMessage(e.toString());
		}

		this.resultString=resultBuffer.toString();
		return resultString;
	}
	
	/** 
     * This method creates a print version of the entries in the wallet
     * @param an object of the class SearchBean
     * @return print version of the wallet entries
     */
	public String exportPrintWallet(SearchBean search) {
		if(search.getWallet().isEmpty()){
			return "<h1>"+this.langObject.getTextFor("wallet.empty")+"</h1>";
		}
		else {
			MediumBean tmp = new MediumBean();
			tmp.setSortWallet(search.getOrderBy());
			
			search.setAnzWalletResults(search.walletSize());
			
			JDBCBean dbCon= new JDBCBean();
			
			Vector<MediumBean> walletElements = new Vector<MediumBean>();
			
			for (int row=0; row < search.getAnzWalletResults(); row++){
				tmp.initializeMedium(search.getWallet().get(row).toString());
				tmp.setSortWallet(search.getOrderBy());
				walletElements.add(tmp);
				tmp = new MediumBean();
			}
			
			Collections.sort(walletElements);
			int row = 0;
			int max = 0;
			int inc = 0;
			int i = 0;
			
			switch(search.getOrderDirection()){
				case 0:	//asc
					row = 0;
					i = 0;
					max = walletElements.size();
					inc = 1;
					break;
				case 1: //desc
					row = walletElements.size()-1;
					i = -walletElements.size();
					max = 0;
					inc = -1;
					break;
				default:
					row = 0;
					i = 0;
					max = walletElements.size();
					inc = 1;
			}
			
			Calendar rightNow = Calendar.getInstance();
			String minute="";
			if (rightNow.get(Calendar.MINUTE)<10) {
				minute ="0"+rightNow.get(Calendar.MINUTE);
			}
			else {
				minute =String.valueOf(rightNow.get(Calendar.MINUTE));
			}
			int monat = rightNow.get(Calendar.MONTH) + 1;
			
			this.resultBuffer.append("<h3>");
			this.resultBuffer.append(this.langObject.getTextFor("header.title"));
			this.resultBuffer.append(this.langObject.getTextFor("export.printDate")+ rightNow.get(Calendar.DATE) + "." + monat + "." + rightNow.get(Calendar.YEAR));
			this.resultBuffer.append(this.langObject.getTextFor("export.printTime")+ rightNow.get(Calendar.HOUR_OF_DAY)+ ":"+ minute);
			this.resultBuffer.append("</h3>");
			this.resultBuffer.append("<ol>");
			
			for (; i < max; i++,row=row+inc){
				tmp = walletElements.get(row);
			
			//for (int row=0; row < res.getRowCount(); row++) {
				this.resultBuffer.append("<li>");
				if (tmp.getLitartSimple().equals("greylit"))
					this.resultBuffer.append(this.langObject.getTextFor("export.greylit")+",  ");
				if (tmp.getLitartSimple().equals("book"))
					this.resultBuffer.append(this.langObject.getTextFor("export.book")+",  ");
				if (tmp.getLitartSimple().equals("artikel"))
					this.resultBuffer.append(this.langObject.getTextFor("export.article")+",  ");
				this.resultBuffer.append("<strong>");
				this.resultBuffer.append(tmp.getAuthorsPrint());
				this.resultBuffer.append("</strong>");
				if (!tmp.getTitle1Print().equals("")) this.resultBuffer.append(",  "+tmp.getTitle1Print());
				if (!tmp.getEditorPrint().equals("")) this.resultBuffer.append(",  "+tmp.getEditorPrint());
				if (!tmp.getYearPrint().equals("")) this.resultBuffer.append(",  "+tmp.getYearPrint());
				if (!tmp.getSignaturPrint().equals("")) this.resultBuffer.append(",  "+tmp.getSignaturPrint());
				if (!tmp.getIsbnPrint().equals("")) this.resultBuffer.append(",  "+tmp.getIsbnPrint());
				if (!tmp.getJournalPrint().equals("")) {
					SQLResults res2 = dbCon.getResultSet("select name from journals where shortcut='"+tmp.getJournalPrint()+"'");
					if (res2.getRowCount() > 0) this.resultBuffer.append(",  in: "+res2.getString(0,"name"));
				}
				this.resultBuffer.append("</li>");
				this.resultBuffer.append("<br>");
			}
			this.resultBuffer.append("</ol>");
		}
		this.resultString=resultBuffer.toString();
		return resultString;
	}
	
	/** 
     * This method creates the print lists for the admin page
     * @param mediaType the type of media which is listed (e.g. book, article and greylit)
     * @param sortBy the sort by criteria
     * @return a list of all media (e.g. book, article and greylit)
     */
	public String exportPrintAdmin(String mediaType, String sortBy) {
		String myQuery="";
		
		SearchBean adminExpSearch = new SearchBean();
		MediumBean tmp = new MediumBean();
		adminExpSearch.setLangObject(this.langObject);
		adminExpSearch.setOrderBy(sortBy);
		adminExpSearch.setSearchWord("*");
		if(mediaType.compareTo("book")==0) adminExpSearch.setCheckboxBook("checked=\"checked\"");
		if(mediaType.compareTo("greylit")==0) adminExpSearch.setCheckboxArticle("checked=\"checked\"");
		if(mediaType.compareTo("artikel")==0) adminExpSearch.setCheckboxGreylit("checked=\"checked\"");
		
		try{
			adminExpSearch.doSearch();
		
			Calendar rightNow = Calendar.getInstance();
			String minute="";
			if (rightNow.get(Calendar.MINUTE)<10) {
				minute ="0"+rightNow.get(Calendar.MINUTE);
			}
			else {
				minute =String.valueOf(rightNow.get(Calendar.MINUTE));
			}
			int monat = rightNow.get(Calendar.MONTH) + 1;
			this.resultBuffer.append("<h3>");
			this.resultBuffer.append(this.langObject.getTextFor("export.printList"));
			if (mediaType.equals("book")){
				this.resultBuffer.append(this.langObject.getTextFor("export.book")+"\"");
			}
			else if (mediaType.equals("artikel")){
				this.resultBuffer.append(this.langObject.getTextFor("export.article")+"\"");
			}
			else if (mediaType.equals("greylit")){
				this.resultBuffer.append(this.langObject.getTextFor("export.greylit")+"\"");
			}
			this.resultBuffer.append(this.langObject.getTextFor("export.printDate")+ rightNow.get(Calendar.DATE) + "." + monat + "." + rightNow.get(Calendar.YEAR));
			this.resultBuffer.append(this.langObject.getTextFor("export.printTime")+ rightNow.get(Calendar.HOUR_OF_DAY)+ ":"+ minute);
			this.resultBuffer.append("</h3>");
			this.resultBuffer.append("<ol style=\"margin-left:20px;\">");
			for (int row=0; row < adminExpSearch.getObjects().size(); row++) {
				tmp = new MediumBean();
				tmp.initializeMedium((adminExpSearch.getObjects().get(row).getPid().split(":"))[1]);
				this.resultBuffer.append("<li>");
				if (mediaType.equals("book"))
					this.resultBuffer.append(tmp.getSignaturPrint()+",  ");
				this.resultBuffer.append(tmp.getAuthorsPrint()+",  ");
				this.resultBuffer.append(tmp.getTitle1Print()+",  ");
				this.resultBuffer.append(tmp.getYearPrint());
				this.resultBuffer.append("</li>");
			}
		}catch(Exception ex){
			System.out.println("Fehler:"+ex.toString());
		}
		this.resultBuffer.append("</ol>");
		this.resultString=resultBuffer.toString();
		return resultString;
	}

	/** 
     * This method creates the BibTex export for one medium
     * @param an object of the class MediumBean
     * @return a BibTex entry for the given medium
     */
	public String exportBibtexDetails(MediumBean medium) {
		if (medium.getLitartExport().equals("book")) {
			this.resultBuffer.append("@book{"+generateBibTexID(medium)+","+"\n");
			this.resultBuffer.append("author = {"+medium.getAuthorsPrint()+"}"+","+"\n");
			this.resultBuffer.append("title = {"+medium.getTitle1Print()+"}"+","+"\n");
			this.resultBuffer.append("publisher = {"+medium.getPublisherPrint()+"}"+","+"\n");
			this.resultBuffer.append("year = {"+medium.getYearPrint()+"}"+","+"\n");
			this.resultBuffer.append("}");
		}	
		else if (medium.getLitartExport().equals("artikel")) {
			this.resultBuffer.append("@article{"+generateBibTexID(medium)+","+"\n");
			this.resultBuffer.append("author = {"+medium.getAuthorsPrint()+"}"+","+"\n");
			this.resultBuffer.append("title = {"+medium.getTitle1Print()+"}"+","+"\n");
			if (!medium.getJournal().equals("")) {
				JDBCBean dbCon = new JDBCBean();
				SQLResults res2 = dbCon.getResultSet("select name from journals where shortcut='"+medium.getJournalPrint()+"'");
				if (res2.getRowCount() > 0) this.resultBuffer.append("journal = {"+res2.getString(0,"name")+"}"+","+"\n");
			}
			this.resultBuffer.append("year = {"+medium.getYearPrint()+"}"+","+"\n");
			this.resultBuffer.append("}");
		}
		else if(medium.getLitartExport().equals("greylit")){	
			this.resultBuffer.append("@unpublished{"+generateBibTexID(medium)+","+"\n");
			this.resultBuffer.append("author = {"+medium.getAuthorsPrint()+"}"+","+"\n");
			this.resultBuffer.append("title = {"+medium.getTitle1Print()+"}"+","+"\n");
			this.resultBuffer.append("year = {"+medium.getYearPrint()+"}"+","+"\n");
			this.resultBuffer.append("}");
		}
		this.resultString = this.resultBuffer.toString();
		return resultString;
	}
	
	/** 
     * This method creates the BibTex export for the wallet entries
     * @param an object of the class SearchBean
     * @return a list of BibTex entries for the medias in the wallet
     */
	public String exportBibtexWallet(SearchBean search) {
		if(search.getWallet().isEmpty()){
			return this.langObject.getTextFor("wallet.empty");
		}
		else {
			MediumBean tmp = new MediumBean();
			tmp.setSortWallet(search.getOrderBy());
			
			search.setAnzWalletResults(search.walletSize());
			
			JDBCBean dbCon= new JDBCBean();
			
			Vector<MediumBean> walletElements = new Vector<MediumBean>();
			
			for (int row=0; row < search.getAnzWalletResults(); row++){
				tmp.initializeMedium(search.getWallet().get(row).toString());
				tmp.setSortWallet(search.getOrderBy());
				walletElements.add(tmp);
				tmp = new MediumBean();
			}
			
			Collections.sort(walletElements);
			int row = 0;
			int max = 0;
			int inc = 0;
			int i = 0;
			
			switch(search.getOrderDirection()){
				case 0:	//asc
					row = 0;
					i = 0;
					max = walletElements.size();
					inc = 1;
					break;
				case 1: //desc
					row = walletElements.size()-1;
					i = -walletElements.size();
					max = 0;
					inc = -1;
					break;
				default:
					row = 0;
					i = 0;
					max = walletElements.size();
					inc = 1;
			}
		
			for (; i < max; i++,row=row+inc){
				tmp = walletElements.get(row);
				if (tmp.getLitartSimple().equals("book")){
					this.resultBuffer.append("@book{"+tmp.getPid()+","+"\n");
					this.resultBuffer.append("author = {"+tmp.getAuthorsPrint()+"}"+","+"\n");
					this.resultBuffer.append("title = {"+tmp.getTitle1Print()+"}"+","+"\n");
					this.resultBuffer.append("publisher = {"+tmp.getPublisherPrint()+"}"+","+"\n");
					this.resultBuffer.append("year = {"+tmp.getYearPrint()+"}"+","+"\n");
					this.resultBuffer.append("}");
					this.resultBuffer.append("\n");
					this.resultBuffer.append("\n");
				}
				else if (tmp.getLitartSimple().equals("artikel")){
					this.resultBuffer.append("@article{"+tmp.getPid()+","+"\n");
					this.resultBuffer.append("author = {"+tmp.getAuthorsPrint()+"}"+","+"\n");
					this.resultBuffer.append("title = {"+tmp.getTitle1Print()+"}"+","+"\n");
					if (!tmp.getJournalPrint().equals("")) {
						SQLResults res2 = dbCon.getResultSet("select name from journals where shortcut='"+tmp.getJournalPrint()+"'");
						if (res2.getRowCount() > 0) this.resultBuffer.append("journal = {"+res2.getString(0,"name")+"}"+","+"\n");
					}
					this.resultBuffer.append("year = {"+tmp.getYearPrint()+"}"+","+"\n");
					this.resultBuffer.append("}");
					this.resultBuffer.append("\n");
					this.resultBuffer.append("\n");
				}
				else if (tmp.getLitartSimple().equals("greylit")){
					this.resultBuffer.append("@unpublished{"+tmp.getPid()+","+"\n");
					this.resultBuffer.append("author = {"+tmp.getAuthorsPrint()+"}"+","+"\n");
					this.resultBuffer.append("title = {"+tmp.getTitle1Print()+"}"+","+"\n");
					this.resultBuffer.append("year = {"+tmp.getYearPrint()+"}"+","+"\n");
					this.resultBuffer.append("}");
					this.resultBuffer.append("\n");
					this.resultBuffer.append("\n");
				}
			}
		}
		this.resultString = this.resultBuffer.toString();
		return resultString;
	}
	
	/** 
     * This method creates the BibTex export for a search query
     * @param an object of the class SearchBean
     * @return a list of BibTex entries for a given search query
     */
	public String exportBibtexSearch(SearchBean search) {
		if (search.getAnzSearchResults() == 0)
			return this.langObject.getTextFor("search.noResults");
		
		JDBCBean dbCon= new JDBCBean();
		
		try{
			search.doSearch();
			List<ObjectFields> searchRes = search.getObjects(); 
			MediumBean tmp;
			String id;		

			for (int row=0; row < searchRes.size(); row++) {
				tmp = new MediumBean();
				id = (searchRes.get(row).getPid().split(":"))[1];
				tmp.initializeMedium(id);
				tmp.setLangObject(this.langObject);
				
				if (tmp.getLitartSimple().equals("book")){
					this.resultBuffer.append("@book{"+tmp.getPid()+","+"\n");
					this.resultBuffer.append("author = {"+tmp.getAuthorsPrint()+"}"+","+"\n");
					this.resultBuffer.append("title = {"+tmp.getTitle1Print()+"}"+","+"\n");
					this.resultBuffer.append("publisher = {"+tmp.getPublisherPrint()+"}"+","+"\n");
					this.resultBuffer.append("year = {"+tmp.getYearPrint()+"}"+","+"\n");
					this.resultBuffer.append("}");
					this.resultBuffer.append("\n");
					this.resultBuffer.append("\n");
				}
				else if (tmp.getLitartSimple().equals("artikel")){
					this.resultBuffer.append("@article{"+tmp.getPid()+","+"\n");
					this.resultBuffer.append("author = {"+tmp.getAuthorsPrint()+"}"+","+"\n");
					this.resultBuffer.append("title = {"+tmp.getTitle1Print()+"}"+","+"\n");
					if (!tmp.getJournalPrint().equals("")) {
						SQLResults res2 = dbCon.getResultSet("select name from journals where shortcut='"+tmp.getJournalPrint()+"'");
						if (res2.getRowCount() > 0) this.resultBuffer.append("journal = {"+res2.getString(0,"name")+"}"+","+"\n");
					}
					this.resultBuffer.append("year = {"+tmp.getYearPrint()+"}"+","+"\n");
					this.resultBuffer.append("}");
					this.resultBuffer.append("\n");
					this.resultBuffer.append("\n");
				}
				else if (tmp.getLitartSimple().equals("greylit")){
					this.resultBuffer.append("@unpublished{"+tmp.getPid()+","+"\n");
					this.resultBuffer.append("author = {"+tmp.getAuthorsPrint()+"}"+","+"\n");
					this.resultBuffer.append("title = {"+tmp.getTitle1Print()+"}"+","+"\n");
					this.resultBuffer.append("year = {"+tmp.getYearPrint()+"}"+","+"\n");
					this.resultBuffer.append("}");
					this.resultBuffer.append("\n");
					this.resultBuffer.append("\n");
				}
		}
		}catch(Exception e){
			search.setErrorMessage(e.toString());
		}
		this.resultString = this.resultBuffer.toString();
		return resultString;
	}		
}
